package TUILayer;
import ControlLayer.*;
import java.util.Scanner;

/**
 * Write a description of class Person here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Person
{

    private AddressCtr addressBookCtr;
    
    public Person()
    {
       addressBookCtr=new AddressCtr();
    }
    
     public void menuAddressBook()
    {
       boolean exit=false;
       while (!exit) //! means while exit not is true (that is: false)
       {
           int choise = printAddressBook();
           switch (choise)
            {
                case 1:
                {
                       showInformationNormal();
                       break;
                }
                case 2:
                {
                       //add person
                       addPerson();
                       break;
                }
                case 3:
                {
                       //remove person
                       deletePerson();
                       break;
                }
                case 4:
                {
                    //Update person
                    updatePerson();
                    break;
                }
                case 5:
                {
                    //list all persons
                    printPersonMenu();
                    break;
                }
                case 6:
                {
                    //return
                    exit = true;        
                    break;
                }
            
            }
        }
    }
    
    private void showInformation(int id)
    {
        if(addressBookCtr.readSize()!=0)
        {
        Scanner keyboard = new Scanner(System.in);
        addressBookCtr.printPerson(id);
        System.out.println(" Press 1 to continue: ");
        boolean exit=false;
        while (!exit)
        {
        int select=keyboard.nextInt();
        if(select==1)
            exit=true;
        }
        }
       else
        {
        System.out.println("List is empty!");
        System.out.println(" Press (1) to continue! ");
        Scanner keyboard = new Scanner(System.in);
        boolean exit=false;
        while (!exit)
        {
        int select=keyboard.nextInt();
        if(select==1)
            exit=true;
        }
        }
    }
    
     private void showInformationNormal()
    {
        if(addressBookCtr.readSize()!=0)
        {
            int id=-1;
       Scanner keyboard = new Scanner(System.in);
       System.out.println("Id:");
       while(!checkID(id))
            id=keyboard.nextInt();
        addressBookCtr.printPerson(id);
        System.out.println(" Press 1 to continue: ");
        boolean exit=false;
        while (!exit)
        {
        int select=keyboard.nextInt();
        if(select==1)
            exit=true;
        }
        }
        else
        {
        System.out.println("List is empty!");
        System.out.println(" Press (1) to continue! ");
        Scanner keyboard = new Scanner(System.in);
        boolean exit=false;
        while (!exit)
        {
        int select=keyboard.nextInt();
        if(select==1)
            exit=true;
        }
        }
    }
    
    private void addPerson()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Id:");
        int id=keyboard.nextInt();
        System.out.println("Name:");
        keyboard.nextLine();
        String name=keyboard.nextLine();
        System.out.println("Address:");
        String address=keyboard.nextLine();
        System.out.println("PostalCode:");
        int postalCode=keyboard.nextInt(); 
        System.out.println("City");
        keyboard.nextLine();
        String city=keyboard.nextLine();
        System.out.println("Phone");
        String phone=keyboard.nextLine();
        addressBookCtr.addPerson(id,name,address,postalCode,city,phone);
        
    }
    
    private int printAddressBook()
    {
       Scanner keyboard = new Scanner(System.in);
       System.out.println("\f *** AddressBook ***");
       System.out.println(" (1)Show information about person");
       System.out.println(" (2)Add person");
       System.out.println(" (3)Delete Person");
       System.out.println(" (4)Update person");
       System.out.println(" (5)List all persons"); 
       System.out.println(" (6)Return"); 
       System.out.print("\n Make your choise: ");
       int choise = keyboard.nextInt();
       return choise;
    }

    private void printPersonMenu()
    {
        if(addressBookCtr.readSize()!=0)
        {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Persons: ");
        addressBookCtr.printPersons();
        System.out.println(" Press (1) to continue! ");
        boolean exit=false;
        while (!exit)
        {
        int select=keyboard.nextInt();
        if(select==1)
            exit=true;
        }
        }
        else
        {
        System.out.println("List is empty!");
        System.out.println(" Press (1) to continue! ");
        Scanner keyboard = new Scanner(System.in);
        boolean exit=false;
        while (!exit)
        {
        int select=keyboard.nextInt();
        if(select==1)
            exit=true;
        }
        }
    }
    
    private void deletePerson()
    {
        if(addressBookCtr.readSize()!=0)
        {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Id:");
        int id=keyboard.nextInt();
        addressBookCtr.removePerson(id);
        System.out.println("Pres (1) to continue.");
        boolean exit=false;
        while (!exit)
        {
        int select=keyboard.nextInt();
        if(select==1)
            exit=true;
        }
        }
        else
        {
        System.out.println("List is empty!");
        System.out.println(" Press (1) to continue! ");
        Scanner keyboard = new Scanner(System.in);
        boolean exit=false;
        while (!exit)
        {
        int select=keyboard.nextInt();
        if(select==1)
            exit=true;
        }
        }
    }
    
    private void updatePerson()
   {   
       if(addressBookCtr.readSize()!=0)
        {
       int id=-5673;
       Scanner keyboard = new Scanner(System.in);
       System.out.println("Id:");
       while(!checkID(id))
            id=keyboard.nextInt();
       showInformation(id);
       boolean exit=false;
       while (!exit) //! means while exit not is true (that is: false)
       {
           int choise = printUpdatePerson();
           switch (choise)
            {
                case 1:
                {
                       //update ID
                      id=updateInt(1,id);
                      break;
                }
                case 2:
                {
                       //update Name
                       updateString(1,id);
                       break;
                }
                case 3:
                {
                       //update address
                       updateString(2,id);
                       break;
                }
                case 4:
                {
                    updateInt(2,id);
                    break;
                }
                case 5:
                {
                    //update city
                    updateString(3,id);
                    break;
                }
                case 6:
                {
                    //update phone number
                    updateString(4,id);
                    break;
                }
                case 7:
                {
                    //return
                    exit = true;        
                    break;
                }
            
            }
        }
        }
        else
        {
        System.out.println("List is empty!");
        System.out.println(" Press (1) to continue! ");
        Scanner keyboard = new Scanner(System.in);
        boolean exit=false;
        while (!exit)
        {
        int select=keyboard.nextInt();
        if(select==1)
            exit=true;
        }
        }
    
        
    }
    
    private boolean checkID(int id)
    {
        return addressBookCtr.checkID(id);
    }
    
    
    private int printUpdatePerson()
    {
       
       Scanner keyboard = new Scanner(System.in);
       System.out.println("\f *** Update Person ***");
       System.out.println(" (1)Update ID");
       System.out.println(" (2)Update Name");
       System.out.println(" (3)Update Address");
       System.out.println(" (4)Update Postal Code");
       System.out.println(" (5)Update City"); 
       System.out.println(" (6)Update Phone Number"); 
       System.out.println(" (7)Return"); 
       System.out.print("\n Make your choise: ");
       int choise = keyboard.nextInt();
       return choise;
    }
    
    private int updateInt(int i,int id)
    {
       Scanner keyboard = new Scanner(System.in);
       System.out.println("New value : ");
       int newValue=keyboard.nextInt();
       keyboard.nextLine();
       if(i==1)
       {
            addressBookCtr.updatePersonID(id,newValue);
            System.out.println("UPDATED!");
       }
       else
       {    
           addressBookCtr.updatePersonPostalCode(id,newValue);
           System.out.println("UPDATED!");
        }
       System.out.println("Pres (1) to continue.");
       boolean exit=false;
       while (!exit)
       {
             int select=keyboard.nextInt();
                 if(select==1)
                    exit=true;
       }
       return newValue;
    }
    
    
    private void updateString(int i,int id)
    {
       Scanner keyboard = new Scanner(System.in);
       System.out.println("New value : ");
       String newValue=keyboard.nextLine();
       switch (i)
       {
           case 1:
           {
            addressBookCtr.updatePersonName(id,newValue);
            System.out.println("UPDATED!");
            break;
        }
        
        case 2:
           {
            addressBookCtr.updatePersonAddress(id,newValue);
            System.out.println("UPDATED!");
            break;
        }
        
        case 3:
           {
            addressBookCtr.updatePersonCity(id,newValue);
            System.out.println("UPDATED!");
            break;
        }
        
        case 4:
           {
            addressBookCtr.updatePersonPhone(id,newValue);
            System.out.println("UPDATED!");
            break;
        }
       }
       System.out.println(" Pres (1) to continue.");
       boolean exit=false;
       while (!exit)
       {
             int select=keyboard.nextInt();
                 if(select==1)
                    exit=true;
       }
       
    }
    
}
